﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QunYing.Common;
using System.IO;
using Spore.Validator;
using System.ServiceModel;

namespace Client
{
    public partial class FormDealerModify : BasePopForm
    {
        private  QunYing.BLL.DealerInfo bll_DearInfo = new QunYing.BLL.DealerInfo();

        QunYing.BLL.UserLoginInfo bll_loginInfo = new QunYing.BLL.UserLoginInfo();
        QunYing.Model.UserLoginInfo logininfo;
       
        QunYing.Model.DealerInfo dealerInfo ;
        public FormDealerModify() 
        {
            InitializeComponent();
            this.FormValidator.SetControlValitors(this.txt_办公室电话, Spore.Validator.ValidatorDefaults.Phone);
            this.FormValidator.SetControlValitors(this.txt_传真, ValidatorDefaults.Phone);
            this.FormValidator.SetControlValitors(this.txt_电子邮箱, ValidatorDefaults.Email);
            this.FormValidator.SetControlValitors(this.txt_技工人数, ValidatorDefaults.Integer);
            this.FormValidator.SetControlValitors(this.txt_技术主管, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_经销商编号, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_经销商地址, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_经销商级别, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(txt_密码, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_名称, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_热线电话, ValidatorDefaults.Phone);
            this.FormValidator.SetControlValitors(this.txt_所在省份, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_主管电话, ValidatorDefaults.Phone);
            this.FormValidator.SetControlValitors(this.txt_总经理, ValidatorDefaults.NotNull);
            
        }

        public void BindData(QunYing.Model.DealerInfo dealerInfo)
        {
            this.dealerInfo = dealerInfo;
            this.txt_办公室电话.Text = dealerInfo.办公室电话;
            this.txt_传真.Text = dealerInfo.传真;
            this.txt_电子邮箱.Text = dealerInfo.电子邮箱;
            this.txt_技工人数.Text = dealerInfo.技工数量.ToString();
            this.txt_技术主管.Text = dealerInfo.技术主管;
            this.txt_经销商简介.Text = dealerInfo.简介;
            this.txt_经销商编号.Text = dealerInfo.编号;
            this.txt_经销商级别.Text = dealerInfo.级别;
            this.txt_名称.Text = dealerInfo.名称;
            this.txt_热线电话.Text = dealerInfo.服务热线;
            this.txt_所在省份.Text = dealerInfo.省份;
            this.txt_主管电话.Text = dealerInfo.主管电话;
            this.txt_资格截止日期.DateTime = dealerInfo.资格截止日期.Value;
            this.txt_资格生效日期.DateTime = dealerInfo.资格生效日期.Value;
            this.txt_总经理.Text = dealerInfo.总经理;
            this.txt_经销商地址.Text = dealerInfo.地址;
            var opera = ClientProxyFactory.GetProxy<IImageOperator>(QunYing.Common.GlobalInfo.ServiceImageOperationDealerInfo);
            Bitmap[] bp = opera.GetImages(this.dealerInfo.编号, new string[] { this.dealerInfo.编号 + ".jpg" });
            if (bp.Length != 0)
            {
                this.pic_门店图片.Image = bp[0];
            }
            logininfo = bll_loginInfo.GetModelList(string.Format("电邮地址='{0}'", dealerInfo.电子邮箱))[0];
            this.txt_密码.Text = logininfo.密码;
            (opera as ICommunicationObject).Close();

        }

        private void btn_保存_Click(object sender, EventArgs e)
        {
            if (!this.FormValidator.ValidAll()) return;
            if (!string.IsNullOrEmpty(this.txt_图片路径.Text.Trim()))
            {
                if (File.Exists(this.txt_图片路径.Text.Trim()))
                {
                    try
                    {
                        QunYing.Common.IImageOperator opera = QunYing.Common.ClientProxyFactory.GetProxy<QunYing.Common.IImageOperator>(QunYing.Common.GlobalInfo.ServiceImageOperationDealerInfo);
                        opera.UpLoad(this.txt_经销商编号.Text, new string[] { this.txt_经销商编号.Text + ".jpg" }, new Bitmap[] { (Bitmap)Bitmap.FromFile(this.txt_图片路径.Text.Trim()) });
                    }
                    catch (Exception ex)
                    {
                        GlobalInfo.MsgBoxShowError(ex.Message);
                        QunYing.Common.Loger.Log.Error("修改经销商图片时发生错误:" + ex.Message);
                    }
                }
                else
                {
                    GlobalInfo.MsgBoxShowError("图片文件路径不对，如果不需要更新图片，图片地址栏请勿填写！");
                    return;
                }
            }
            try
            {
             
                logininfo.密码 = this.txt_密码.Text;
                logininfo.手机号码 = this.txt_办公室电话.Text;
                 this.bll_loginInfo.Update(logininfo);
                 var dinfo = this.dealerInfo;
                dinfo.办公室电话 = this.txt_办公室电话.Text;
                dinfo.传真 = this.txt_传真.Text;
                dinfo.地址 = this.txt_经销商地址.Text;
                dinfo.电子邮箱 = this.txt_电子邮箱.Text;
                dinfo.服务热线 = this.txt_热线电话.Text;
                dinfo.级别 = this.txt_经销商级别.Text;
                dinfo.技工数量 = Int32.Parse(this.txt_技工人数.Text);
                dinfo.技术主管 = this.txt_技术主管.Text;
                dinfo.简介 = this.txt_经销商简介.Text;
                dinfo.经度 = string.Empty;
                dinfo.纬度 = string.Empty;
                dinfo.省份 = this.txt_所在省份.Text;
                dinfo.名称 = this.txt_名称.Text;
                dinfo.主管电话 = this.txt_主管电话.Text;
                dinfo.资格截止日期 = this.txt_资格截止日期.DateTime;
                dinfo.资格生效日期 = this.txt_资格生效日期.DateTime;
                dinfo.总经理 = this.txt_总经理.Text;
                this.bll_DearInfo.Update(dinfo);
            }
            catch (Exception ex)
            {
                GlobalInfo.MsgBoxShowError("更新经销商信息时发生错误：" + ex.Message);
                QunYing.Common.Loger.Log.Error("更新经销商信息时发生错误:" + ex.Message);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void btn_浏览_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckPathExists = true;
            ofd.Multiselect = false;
            ofd.Filter = string.Format("{0}|{1};{2};{3}", "图片文件", "*.jpg", "*.pgn", "*.bmp");
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.txt_图片路径.Text = ofd.FileName;
                this.pic_门店图片.Image = Bitmap.FromFile(txt_图片路径.Text);
            }
        }

        private void btn_关闭_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}